//
//  WBHomeViewModel.swift
//  微博Demo
//
//  Created by 花化华 on 2016/11/27.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

//MARK: - 布局的数据结构体
struct StatusLayout {
    //间隔
    let margin: CGFloat = 12
    //用户头像size
    let iconSize: CGSize = CGSize(width: 35, height: 35)
    //vip level Icon的大小
    let littleIconSize: CGSize = CGSize(width: 15, height: 15)
    //每行几张图片
    let numPerRow: Int = 3
    //图片的size
    let imageSize: CGSize
    
    // ??
    init () {
        let imageWidth: CGFloat = (screenWidth - CGFloat(numPerRow+1) * margin)/CGFloat(numPerRow)
        imageSize = CGSize(width: imageWidth, height: imageWidth)
    }
}

//布局中常用的数据结构体
let statusLayout = StatusLayout()

//MARK: - 属性, 构造函数, 生命周期函数
class WBHomeViewModel: NSObject {

    /// ViewModel
    var statusModel: WBStatusModel
    /// user的vip等级图标
    var vipImage: UIImage?
    /// user的皇冠等级图标
    var levelImage: UIImage?
    /// 来源文字
    var sourceText: String?
    /// 时间文字
    var timeText: String?
    /// 转发微博文字
    var retweeted_text: String?
    /// 配图视图的大小(原创或转发)
    var pictureViewSize: CGSize = CGSize.zero
    /// 配图的url(原创或转发只可能有一个有图片,如果原创有图片,存储原创的,如果转发有,存储转发的)
    var picUrls: [WBPictureModel]?
    /// cell的行高
    var cellHeight: CGFloat = 0
    
    
    /// 构造函数, 用model构造 ViewModel
    init (statusModel: WBStatusModel) {
//        print("userVip: \(statusModel.user?.verified_type), userlevel: \(statusModel.user?.mbrank)")
        self.statusModel = statusModel
        
        super.init()
        
        //处理用户细节: 皇冠,等级
        dealWithUser()
        //处理微博细节: 来源, 时间
        dealWithStatus()
        //设置picUrls的值
        setPicurls()
        //计算配图的大小
        caculatePictureViewSize()
    }
    
}


// MARK: - 计算行高
extension WBHomeViewModel {
    
    //计算行高
    func caculateRowHeigh (){
        //高度
        var height: CGFloat = 0
        //微博文字size
        let testSize = CGSize(width: screenWidth - statusLayout.margin*2, height: CGFloat(MAXFLOAT))
        
        //>a: 原创微博
        //1. cell和cell 之间的间隔
        height += 5
        //2. 原创文字和顶部的间隔
        height += statusLayout.margin
        //3. 用户头像高度
        height += statusLayout.iconSize.height
        //4. 原创微博文字的顶部的margin
        height += statusLayout.margin
        
        //5. 原创微博的文字高度
        if let text = statusModel.text {
            let statusTextSize = (text as NSString).boundingRect(with: testSize, options:.usesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 15)], context: nil).size
            height += statusTextSize.height
        }
        height += statusLayout.margin
        
        //6.原创的配图的高度
        if let pic_urls = statusModel.pic_urls, pic_urls.count > 0 {
            //配图和文字的间隔
            height += statusLayout.margin
            //配图的高度
            height += pictureViewSize.height
        }
        
        //>b: 转发微博
        if let _ = statusModel.retweeted_status {
            //转发微博的文字的top
            height += statusLayout.margin
            
            //转发微博的文字高度
            if let text = retweeted_text {
                let retweetedTextSize = (text as NSString).boundingRect(with: testSize, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: UIFont.systemFont(ofSize: 15)], context: nil).size
                height += retweetedTextSize.height
            }
            
            //底部的bottom
            height += statusLayout.margin
            
            //转发图片
            if let pic_urls = statusModel.retweeted_status?.pic_urls, pic_urls.count > 0 {
                //配图和文字的间隔
                height += statusLayout.margin
                //配图的高度
                height += pictureViewSize.height
            }
        }
        
        //>c: toolBar
        height += 36
        
        //赋值结果
        cellHeight = height
    }
    
    //计算行高之前,保证所有的子控件的高度已经被确定了.
//    func caculateCellHeight () {}

}


// MARK: - 处理细节
extension WBHomeViewModel {

    //处理用户细节: 皇冠,等级
    func dealWithUser() {
        //1. 处理vip图标
        if let verified_type = statusModel.user?.verified_type {
            switch verified_type {
            case 0:
                vipImage = UIImage(named: "avatar_vip")
            case 2, 3, 5:
                vipImage = UIImage(named: "avatar_enterprise_vip")
            case 220:
                vipImage = UIImage(named: "avatar_grassroot")
            default:vipImage = nil
            }
        }
        
        //2. 处理皇冠等级
        if let mbrank = statusModel.user?.mbrank, mbrank > 0 && mbrank < 7  {
            levelImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
    }
    
    //处理微博细节: 来源, 时间
    func dealWithStatus() {
        // 处理来源字符串
        if let source = statusModel.source {
            //上界
            let startIndex = source.range(of: "\">")?.upperBound
            //下界
            let endIndex = source.range(of: "</a>")?.lowerBound
            
            if let startIndex = startIndex, let endIndex = endIndex {
                sourceText = source.substring(with: startIndex..<endIndex)
            }
        }
        
        //处理时间字符串
        if let created_at = statusModel.created_at {
            timeText = Date.sinaTimeStringToDate(sinaTimeString: created_at).sinaDateToShowTime()
        }
        
        //处理转发微博文字
        if let text = statusModel.retweeted_status?.text, let userName = statusModel.retweeted_status?.user?.screen_name {
            retweeted_text = "@\(userName): \(text)"
        }
    }
    
}


// MARK: - 设置图片相关
extension WBHomeViewModel {
    
    //把原创或转发的配图模型数据赋值给 picUrls  **或
    func setPicurls() {
        //转发有配图, 把转发的配图模型数据赋给 picUrls
        if let pic_urls = statusModel.retweeted_status?.pic_urls, pic_urls.count > 0 {
            picUrls = pic_urls
        }
        
        //原创微博
        if let pic_urls = statusModel.pic_urls, pic_urls.count > 0  {
            picUrls = pic_urls
        }
    }
    
    //计算配图的大小
    func caculatePictureViewSize() {
        if let pic_urls = picUrls, pic_urls.count > 0 {
            //一共有多少行  3张／行
            let rows = (pic_urls.count - 1)/3 + 1
            //配图框高
            let imageWidth = (screenWidth - 4*statusLayout.margin)/3
            
            //配图视图的size
            pictureViewSize = CGSize(width: screenWidth - 2 * statusLayout.margin, height:CGFloat(rows)*imageWidth + CGFloat(rows - 1) * statusLayout.margin)
        }
    }
    
}
